var express = require('express')
var router = express.Router();
var User = require('../model/User')
var jwt = require('jsonwebtoken');
var bcrypt = require('bcryptjs')
var passport = require('passport');
//注册
router.post('/register', (req, res) => {
    User.findOne({
        email: req.body.email
    }).then(result => {
        if (result) {
            return res.status.json({ msg: '邮箱已经被注册' })
        } else {
            var salt = bcrypt.genSaltSync(10);
            var hash = bcrypt.hashSync(req.body.password, salt);
            var newUser = new User({
                username: req.body.username,
                password: hash,
                email: req.body.email,
                education: req.body.education,
                age: req.body.age,
                sex: req.body.sex,
                avater: req.body.avater,
                sexOrFriends: req.body.sexOrFriends,
                projectName: req.body.projectName,
                projectTime: req.body.projectTime,
                projectRemark: req.body.projectRemark,
                projectContent: req.body.projectContent,
                prodectAddress: req.body.prodectAddress,
                hobbies: req.body.hobbies,
                isZainan: req.body.isZainan,
                appointment: req.body.appointment,
                game: req.body.game,
                hobbiesInfo: req.body.hobbiesInfo,
            })
            newUser.save().then(result => {
                res.status(200).json({ msg: '帐号注册成功' })
            })
        }
    })
})
//登录
router.post('/login', (req, res) => {
    var email = req.body.email;
    var password = req.body.password;
    User.findOne({
        email: email
    }).then(user => {
        if (user) {
            console.log(user)
            if (user.isStatus) {
                bcrypt.compare(password, user.password).then(function (isMatch) {
                    if (isMatch) {
                        //储存token
                        //自定义规则
                        var rule = {
                            id: user.id,
                            name: user.username,
                            email: user.email,
                            isAdmin: user.isAdmin,
                            avater: user.avater
                        };
                        // jwt.sign('规则','加密名字','过期时间','函数')
                        jwt.sign(rule, 'secret', {
                            expiresIn: 3600
                        }, function (err, token) {
                            if (err) throw err;
                            res.json({
                                success: true,
                                type: 'success',
                                msg: '登录成功',
                                token: 'Bearer ' + token
                            })
                        })
                    } else {
                        return res.json({
                            type: 'error',
                            msg: '帐号或密码错误'
                        })
                    }
                })
            } else {
                return res.json({
                    type: 'error',
                    msg: '帐号已被禁用，请联系管理员'
                })
            }

        } else {
            return res.json({
                type: 'error',
                msg: '帐号不存在，请去注册'
            })
        }
    })
})
module.exports = router;